﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;
using System.Collections.ObjectModel;

namespace Chinese2Pinyin
{
    /// <summary>
    /// Convert chinese character to Pin-Yin
    /// </summary>
    public class Chinese2Pinyin
    {
        /// <summary>
        /// Convert Chinese character to Pin-Yin or the first letter of Pin-Yin
        /// </summary>
        /// <param name="chinese">Chinese string</param>
        /// <param name="separator">The separator of every Pin-Yin</param>
        /// <param name="isFirstLetter">Where or not the first letter of Pin-Yin</param>
        /// <returns>The Pin-Yin or the first letter of Pin-Yin</returns>
        public static string Convert(string chinese, string separator, bool isFirstLetter)
        {
            if (chinese == null)
            {
                return null;
            }
            StringBuilder sb = new StringBuilder();
            foreach (char c in chinese)
            {
                if (ChineseChar.IsValidChar(c))
                {
                    ChineseChar cc = new ChineseChar(c);
                    ReadOnlyCollection<string> pinyin = cc.Pinyins;
                    if (pinyin.Count == 0)
                    {
                        continue;
                    }
                    string s = pinyin[0]; //可能是多音字, 如果是多音字, 只取第一个
                    if (isFirstLetter)
                    {
                        s = s.Substring(0, 1);
                    }
                    else
                    {
                        s = s.Substring(0, s.Length - 1); //去掉最后一个音调
                    }
                    if (separator != null && sb.Length > 0)
                    {
                        sb.Append(separator);
                    }
                    sb.Append(s);
                }
                else
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// Convert Chinese character to Pin-Yin
        /// </summary>
        /// <param name="chinese">Chinese string</param>
        /// <param name="separator">Pin-Yin's separator</param>
        /// <returns></returns>
        public static string Convert2Pinyin(string chinese, string separator)
        {
            return Convert(chinese, separator, false);
        }
        /// <summary>
        /// Convert Chinese character to Pin-Yin
        /// </summary>
        /// <param name="chinese"></param>
        /// <returns></returns>
        public static string Convert2Pinyin(string chinese)
        {
            return Convert2Pinyin(chinese, null);
        }
        /// <summary>
        /// Convert Chinese character to the first letter of Pin-Yin
        /// </summary>
        /// <param name="chinese"></param>
        /// <returns></returns>
        public static string Convert2FirstLetter(string chinese)
        {
            return Convert(chinese, null, true);
        }
    }
}
